package com.technology.education.ui.teacher.home

import androidx.lifecycle.MutableLiveData
import com.technology.education.data.ListModel
import com.technology.education.data.model.*
import com.technology.education.ext.sendTakeOrder
import com.technology.education.ext.toast
import com.technology.education.network.apiService
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.BooleanObservableField
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.state.ResultState

class HomeViewModel : BaseViewModel() {
    val status = StringObservableField("获取中")
    val showMenu = BooleanObservableField(false)
    val homeResult = MutableLiveData<ResultState<TeacherHomeModel>>()
    val orderResult = MutableLiveData<ResultState<ListModel<TeachOrderModel>>>()
    val takeResult = MutableLiveData<ExchangeGoodsModel>()
    val result = MutableLiveData<ResultState<DurationWithdrawModel>>()

    override fun getData() {
        super.getData()
        request({
            apiService.pendingOrderList(
                hashMapOf(
                    "pageNo" to pageIndex,
                    "pageSize" to pageSize
                )
            )
        }, orderResult)
    }

    override fun refresh() {
        super.refresh()
        getHomeData()
    }

    /**
     * banner 图,接单状态等
     */
    fun getHomeData() {
        request({ apiService.homeTeacher() }, homeResult)
    }

    fun takeOrder(id: String, receiver: String) {
        request({ apiService.takeOrder(hashMapOf("id" to id)) }, success = {
            it.id=id
            takeResult.postValue(it)
            sendTakeOrder(id, receiver)
        }, error = {
            toast(it.errorMsg)
        }, isShowDialog = true)
    }

    fun commitOrder() {
        request({ apiService.placeDepositOrder() }, result, isShowDialog = true)
    }

    /**
     * 更换当前状态
     */
    fun changeStatus(type: Int) {
        showMenu.set(false)
        request({ apiService.updateServiceState(hashMapOf("serviceState" to type)) }, success = {
            status.set(if (type == 0) "休息中" else "接单中")
        }, error = {
            toast(it.errorMsg)
        })

    }

    /**
     * 切换状态菜单展示
     */
    fun toggleMenu() {
        showMenu.set(!showMenu.get())
    }
}